package com.thx.file;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import com.thx.common.web.SessionInfo;
import com.thx.common.web.WebConstant;
public class TokenUtil
{
  protected static final Logger logger = Logger.getLogger(TokenUtil.class);
  /*生成token串 */
  public static String generateToken(String name, String size,HttpServletRequest req,String isLogin,String modPath)
    throws IOException
  {
	  HttpSession session = req.getSession();
	  SessionInfo user = (SessionInfo)session.getAttribute(WebConstant.KEY_USER);
      if ((name == null) || (size == null)){
         return "";
      }
      int code = name.hashCode();
      try {//生成token
        StringBuffer token = new StringBuffer(code > 0 ? "A" : "B").append(Math.abs(code));
        if("true".equals(isLogin)){
        	token.append(user.getUserId());
        }
        token.append("_"+size.trim());
        IoUtil.storeToken(token.toString(),modPath);//生成用token串命名的文件
        return token.toString();
      } catch (Exception e) {
    	  logger.error(e.toString());
    	  throw new IOException();
    }

  }
}